﻿using System;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using DeepZoomPivotConstructor.ErrorAndLoggingInterfaces;
using DeepZoomPivotConstructor.VisualLib;
using IndicoInterface;
using RenderEngineInterfaces;

namespace IndicoRender
{
    /// <summary>
    /// Attempt to render a Uri that points to a
    /// indico agenda system.
    /// </summary>
    [Export(typeof(IRenderItem))]
    [RenderInfo(typeof(Uri))]
    public class RenderIndicoWebSite : IRenderItem
    {
        /// <summary>
        /// How we do the actual rendering
        /// </summary>
        [Import]
        private IRenderEngine MyRender { get; set; }

        [Import]
        private IErrorLogger MyErrorLogger { get; set; }

        /// <summary>
        /// Download the agenda and then send the XML off to be rendered. We could include
        /// the code here to do that, however, doing this makes it much easier to test
        /// other parts of the agenda system without having to be online.
        /// </summary>
        /// <param name="what"></param>
        /// <returns></returns>
        public Task<Visual> Render(object what)
        {
            return Task<Visual>.Factory.StartNew(() =>
                {
                    Uri u = what as Uri;
                    AgendaInfo ai = new AgendaInfo(u.OriginalString);
                    return MyRender.Render(ai);
                });
        }

        /// <summary>
        /// See if we can figure out if this is indico from the URL!
        /// </summary>
        /// <param name="what"></param>
        /// <returns></returns>
        public RenderPluginPriority RenderPriority(object what)
        {
            Uri u = what as Uri;
            try
            {
                AgendaInfo info = new AgendaInfo(u.OriginalString);

                // If we are already talking about a contribution, then this render
                // is not the right one!

                if (u.OriginalString.Contains("contribId") || u.OriginalString.Contains("contribid"))
                    return RenderPluginPriority.RPPCantDoIt;

                //
                // This looks like an indico meeting agneda!
                //

                return RenderPluginPriority.RPPSpecific;
            }
            catch (AgendaException)
            {
                return RenderPluginPriority.RPPCantDoIt;
            }
        }
    }
}
